#!/bin/bash

# 获取传入的参数
ark_cmd=$1
ark_mode=$(grep '^ark_mode=' ~/.bashrc | cut -d '=' -f 2 | tr -d '\"')

# 常规文件路径
hub_path="../../standalone"
lib_path="LD_LIBRARY_PATH=$hub_path/out/x64.$ark_mode/thirdparty/icu:../standalone/out/x64.debug/arkcompiler/ets_runtime:../standalone/out/x64.debug/thirdparty/icu:../standalone/out/x64.debug/thirdparty/zlib"
es2abc_path="$hub_path/out/x64.$ark_mode/arkcompiler/ets_frontend/es2abc"
vm_path="$hub_path/out/x64.$ark_mode/arkcompiler/ets_runtime/ark_js_vm"
aot_path="$hub_path/out/x64.$ark_mode/arkcompiler/ets_runtime/ark_aot_compiler"

# 定义颜色代码
RED='\033[1;31m'  # 红色
GREEN='\033[1;32m'  # 绿色
YELLOW='\033[1;33m'  # 黄色
BLUE='\033[1;34m' # 蓝色
RESET='\033[0m'      # 重置颜色

# 打印并执行 函数
echo_and_eval() {
    local cmd="$@"  # 获取传入的命令
    echo -e "${GREEN}[Running] $cmd${RESET}"  # 如果成功，打印 success
    eval "$cmd"  # 执行命令
    if [ $? -eq 0 ]; then
        :   # 相当于pass
    else
        echo -e "${RED}Error while executing: $cmd${RESET}"  # 如果失败，打印 fail
        exit 1  # 如果命令失败，退出脚本
    fi
}

# 校验执行目录
if [ $(basename "$PWD") != "ets_runtime" ]; then
    echo -e "${RED}Error: 需要切换执行目录  cd /home/xuan/ohos/ark_tools/ets_runtime ${RESET}"
    exit 1
fi

# 文件编译执行  函数
get_gdb() {
  if [ "$1" == "gdb" ]; then
    gdb="gdb --args"
  else
    gdb=""
  fi
  echo "$gdb"
}
abc_step() {
    para=(
        --module
        --merge-abc
        --output t.abc
    )
    echo_and_eval $(get_gdb $1) $es2abc_path "${para[@]}" t.ts
}
pgo_step() {
    para=(
        --entry-point=t
        --enable-pgo-profiler=true
        # --log-level=info
        --compiler-pgo-profiler-path=t.ap
    )
    echo_and_eval $(get_gdb $1) $vm_path "${para[@]}" t.abc
}
jit_step() {
    para=(
        --entry-point=t
        --compiler-enable-litecg=false
        --compiler-enable-jit=true
        --compiler-jit-call-threshold=1
        --enable-force-gc=false
        # --icu-data-path="../standalone/third_party/icu/ohos_icu4j/data"
        --compiler-trace-deopt=true
        --log-level=info
    )
    echo_and_eval $(get_gdb $1) $lib_path $vm_path "${para[@]}" t.abc
}
aot_cpl_step() {
    para=(
        --compiler-pgo-profiler-path=t.ap \
        --aot-file=t \
        --log-level=info \
    )
    echo_and_eval $(get_gdb $1) $lib_path $aot_path "${para[@]}" t.abc
}
aot_step() {
    para=(
        --entry-point=t \
        --aot-file=t \
        # --log-level=info \
    )
    echo_and_eval $(get_gdb $1) $vm_path "${para[@]}" t.abc
}



# ark指令
if [ "$ark_cmd" == "init" ]; then
    echo_and_eval cd $hub_path
    echo_and_eval repo init -u https://gitee.com/ark-standalone-build/manifest.git -b master  # 蓝区独立仓
    echo_and_eval repo sync -c -j16  # 初始多进程下载可能报错，有些先后校验问题，报错后单进程执行repo sync -c
    echo_and_eval "repo forall -c 'git lfs pull'"  # 里面有特殊字符的加双引号 作为完整字符串传入 防止提前解析

elif [ "$ark_cmd" == "sync" ]; then
    echo_and_eval cd $hub_path
    echo_and_eval "repo forall -c 'git reset --hard && git clean -fxd'"  # 清理所有未跟踪文件和目录
    echo_and_eval repo sync -c --no-tags -j16 -d --force-remove-dirty  # 强制同步

elif [ "$ark_cmd" == "rm" ]; then
    echo_and_eval rm -rf t.abc t.ap t.ai t.an

elif [ "$ark_cmd" == "build_pre" ]; then
    echo_and_eval cd $hub_path
    echo_and_eval ./prebuilts_download.sh

elif [ "$ark_cmd" == "build_clangd" ]; then
    out_dir="./"
    echo_and_eval $hub_path/prebuilts/build-tools/linux-x86/bin/gn gen "$out_dir" --args="target_os=\"linux\" target_cpu=\"x64\" is_debug=true" --export-compile-commands

elif [ "$ark_cmd" == "build" ]; then
    echo_and_eval cd $hub_path
    echo_and_eval python ark.py x64.$ark_mode

elif [ "$ark_cmd" == "debug" ]; then
    echo_and_eval perl -pi -e 's/ark_mode=\"release\"/ark_mode=\"debug\"/g' ~/.bashrc
    echo_and_eval source ~/.bashrc
    exec bash  # 用新shell 替换 当前shell

elif [ "$ark_cmd" == "release" ]; then
    echo_and_eval perl -pi -e 's/ark_mode=\"debug\"/ark_mode=\"release\"/g' ~/.bashrc
    echo_and_eval source ~/.bashrc
    exec bash  # 用新shell 替换 当前shell

elif [ "$ark_cmd" == "abc" ]; then
    echo_and_eval rm -f t.abc
    abc_step $2

elif [ "$ark_cmd" == "pgo" ]; then
    echo_and_eval rm -f t.ap
    pgo_step $2

elif [ "$ark_cmd" == "aot_cpl" ]; then
    echo_and_eval rm -f t.an t.ai
    aot_cpl_step $2

elif [ "$ark_cmd" == "aot" ]; then
    echo_and_eval rm -f t.an t.ai
    aot_step $2

elif [ "$ark_cmd" == "all" ]; then
    echo_and_eval rm -f t.abc t.ap t.an t.ai
    abc_step
    pgo_step
    aot_cpl_step
    aot_step

elif [ "$ark_cmd" == "jit" ]; then
    jit_step $2

elif [ "$ark_cmd" == "cherry-pick" ]; then
    echo_and_eval cd $hub_path/arkcompiler/ets_runtime
    echo_and_eval git fetch git@gitee.com:openharmony/arkcompiler_ets_runtime.git pull/$2/head  # pr id
    echo_and_eval git FETCH_HEAD
    echo_and_eval git reset --soft HEAD~1

else 
    echo -e "${RED}Error: 当前ark指令未定义${RESET}"
fi

